package login;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import base.BaseActivity;
import base.httputils.JSONUtils;

import androidx.annotation.Nullable;

import com.mobile.auth.gatewayauth.PhoneNumberAuthHelper;
import com.mobile.auth.gatewayauth.ResultCode;
import com.mobile.auth.gatewayauth.TokenResultListener;
import com.mobile.auth.gatewayauth.model.TokenRet;

/**
 * 进app直接登录的场景
 */
public class OneKeyLoginActivity extends BaseActivity {
    private static final String TAG = OneKeyLoginActivity.class.getSimpleName();

    private PhoneNumberAuthHelper mPhoneNumberAuthHelper;
    private TokenResultListener mTokenResultListener;
    private ProgressDialog mProgressDialog;
    public static final String AUTH_SECRET = "A1k1xuainJFM/LLfjsMDLKiLmg7Imd/ll5lAAlQgGI7k4M91mmUpPH7+UQXz+VzvciRTrIkRjOerCPi1kugZwCRTURYNNp/r/B7E+v5YcfJ0DESfCvp0T9caslANf/cFushC+2wc1w5FUw/4VU2VO81BwbEo7YO5ko+hj4epVOaD8ibA4UznZOiWsIt3WD8pi4bYYAtadic3Izj8gWcm5WPayPUN7OtEJ9szcTDapBSG6W2QhqNQdeAzyj6+L03wm8VkkScBcf0t5oAkQv2Te0wwWE1vsvO6L4CeKNjALeQZ7+b3Nvq7Ww==";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sdkInit(AUTH_SECRET);
        oneKeyLogin();
    }


    public void sdkInit(String secretInfo) {
        Log.e("TAG", "唤起授权页成功：" + "31231");
        mTokenResultListener = new TokenResultListener() {
            @Override
            public void onTokenSuccess(String s) {
                hideLoadingDialog();
                TokenRet tokenRet = null;
                try {
                    tokenRet = TokenRet.fromJson(s);
                    if (ResultCode.CODE_START_AUTHPAGE_SUCCESS.equals(tokenRet.getCode())) {
                        Log.e("TAG", "唤起授权页成功：" + s);
                    }

                    if (ResultCode.CODE_SUCCESS.equals(tokenRet.getCode())) {
                        Log.e("TAG", "获取token成功：" + s);
//                        LogInPlugin.result.success(tokenRet);
                        getResultWithToken(tokenRet.getToken());
                        mPhoneNumberAuthHelper.setAuthListener(null);
                    }
                } catch (Exception e) {
                    Log.e("TAG",  e.toString());
                    e.printStackTrace();
                }
            }

            @Override
            public void onTokenFailed(String s) {
                LogInPlugin.result.success("1");
                Log.e(TAG, "获取token失败：" + s);
                hideLoadingDialog();
                TokenRet tokenRet = null;
                try {
                    tokenRet = TokenRet.fromJson(s);
                    if (ResultCode.CODE_ERROR_USER_CANCEL.equals(tokenRet.getCode())) {
                        //模拟的是必须登录 否则直接退出app的场景
                        finish();
                    } else {
                        hideLoadingDialog();
                String   message="一键登录失败切换到其他登录方式";
            if (tokenRet.getCode().equals("600005")||tokenRet.getCode().equals("600011")){//请切换到其他方式登录
                LogInPlugin.result.success("-100");
                message="切换到其他方式登录";
            }else if (tokenRet.getCode().equals("600008")){//请先开启移动数据网络
                message="请先开启移动数据网络后重试";
            }else if (tokenRet.getCode().equals("600009")||tokenRet.getCode().equals("600007")){//未检测到SIM卡
                message="请先检查SIM卡后重试";
            }

                        Toast.makeText(getApplicationContext(), ""+message, Toast.LENGTH_SHORT).show();
//                        Intent pIntent = new Intent(OneKeyLoginActivity.this, MessageActivity.class);
//                        startActivityForResult(pIntent, 1002);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mPhoneNumberAuthHelper.setAuthListener(null);
            }
        };
        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(this, mTokenResultListener);
        mPhoneNumberAuthHelper.getReporter().setLoggerEnable(true);
        mPhoneNumberAuthHelper.setAuthSDKInfo(secretInfo);
    }

    /**
     * 进入app就需要登录的场景使用
     */
    private void oneKeyLogin() {
        mPhoneNumberAuthHelper = PhoneNumberAuthHelper.getInstance(getApplicationContext(), mTokenResultListener);
        mPhoneNumberAuthHelper.checkEnvAvailable();

        getLoginToken(5000);
    }

    /**
     * 拉起授权页
     * @param timeout 超时时间
     */
    public void getLoginToken(int timeout) {
        mPhoneNumberAuthHelper.getLoginToken(this, timeout);
        showLoadingDialog("正在唤起授权页");
    }


    public void getResultWithToken(final String token) {
        LogInPlugin.result.success(token);

//        Toast.makeText(this, ""+token, Toast.LENGTH_SHORT).show();
    }






    public void showLoadingDialog(String hint) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        mProgressDialog.setMessage(hint);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }

    public void hideLoadingDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
